<?php
namespace BoxEdu\Controller\Home;
class CommonController extends \HomeController {
	public function _initialize() {
		parent::_initialize();
		\Think\Hook::listen('BoxEdu_begin'); //
		$this->nav_cur = "Home";
		//$this->class_list = BoxModel("EduSubject")->class_list();
		$this->header_nav = "Public:header";
		$this->public_header = "Public:head";
		$this->web_title = "和大师兄一起学编程";
		//微信浏览器
		if (!checkAgent()) {
			$this->wxAgent = 1;
		}

		//echo $_SERVER['HTTP_USER_AGENT'];
		//	dump($this->user_info);
		//echo md5('123456');exit();
		//  $this->getHeader();
		//dump($this->user_info);exit();
	}

	public function getHeader() {
		// $header = "./Apps/" . C('DEFAULT_MODULE') . "/View/Home/Public/head_cache.html";
		$headerReal = "./Apps/" . C('DEFAULT_MODULE') . "/View/Home/Public/head.html";
		if (APP_DEBUG === true) {
			$this->resCache($headerReal, "BoxEdu");
		}
	}

	public function ckeck_students_info() {
		if (ACTION_NAME != "students_info" && ACTION_NAME != "get_ajax_school_sel") {
			$r = BoxModel("edu_students")->where("user_id='$this->user_id'")->find();
			if (!$r['school_id']) {
				$this->jump("", U("User/students_info"));
			}
		}
	}

	public function show_subject($dir) {
		$this->page_content = self::fetch($dir);
		$templateFile = $this->getTemplateFile("Subject/home");
		$this->page_content = parent::fetch($templateFile);
		$templateFile = $this->getTemplateFile("Index/home");
		if (IS_AJAX || I('requestFrom') == 'modal') {
			echo $this->page_content;
		} else {
			parent::display($templateFile);
		}

	}

	/****display模板***/
	public function display($dir) {
		if (!is_file($dir)) {
			$templateFile = $this->getTemplateFile("Index/home");
			$this->page_content = self::fetch($dir);
			if (IS_AJAX || I('requestFrom') == 'modal') {
				echo $this->page_content;
			} else {
				parent::display($templateFile);
			}
		} else {
			parent::display($dir);
		}
	}

	public function getTemplateFile($templateFile) {
		//dump( $this->is_mobile);
		$tpl = BOX_MODULE_NAME;
		if ($this->is_mobile) {
			$tpl = "Wap";
		}
		if (!checkAgent()) {
			$tpl = "Wap";
		}
		C('DEFAULT_V_LAYER', "View/" . $tpl);
		if ($templateFile == "" || !is_file($templateFile)) {
			$depr = C('TMPL_FILE_DEPR');
			if (!$templateFile) {
				$templateFile = CONTROLLER_NAME . $depr . ACTION_NAME;
			}
		}
		$File = "./Apps/" . C('DEFAULT_MODULE') . "/View/" . $tpl . "/" . $templateFile . C('TMPL_TEMPLATE_SUFFIX');
		if ($tpl != "Home" && is_file($File)) {
			return $File;
		} else {
			return "./Apps/" . C('DEFAULT_MODULE') . "/View/Home/" . $templateFile . C('TMPL_TEMPLATE_SUFFIX');
		}

	}

	/****fetch模板***/
	public function fetch($templateFile, $content = '', $prefix = '') {
		$templateFile = $this->getTemplateFile($templateFile);
		return parent::fetch($templateFile, $content, $prefix);
	}

	/*****用户中心页面***/
	function show_user($page) {
		$templateFile = $this->getTemplateFile("Index/home");
		$this->user_page_content = self::fetch($page);
		if (IS_AJAX && I('requestFrom') == 'modal') {
			echo $this->user_page_content;
		} else {
			$this->page_content = self::fetch("User/index");
			parent::display($templateFile);
		}
	}

}
